<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <!-- 引入layer的css 和 js -->
  <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.js"></script>
  <script src="./libs/layui/lay/modules/layer.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <!-- 1.加id -->
        <tbody id="tbody">

          <tr class="danger">
            <td>中周</td>
            <td>How are you?</td>
            <td>《世界，你好》</td>
            <td>2017-07-09 18:22:00</td>
            <td>待审核</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>


        </tbody>
      </table>
      <div id="pager" class="row text-center">
        <!-- 此处用来存放分页控件的静态结构 -->
      </div>

    </div>
  </div>
  <!-- 2.引入 -->
  <script src="./libs/art-template/template-web.js"></script>
  <script src="./js/http.js"></script>
  <script id="art_list" type="text/html">
    {{each data}}
    {{if $value.state=='已拒绝'}}
    <tr class="danger">
      {{else}}
      <tr>
      {{/if}}
      <td>{{$value.author}}</td>
      <td>{{$value.content}}</td>
      <td>{{$value.title}}</td>
      <td>{{$value.date}}</td>
      <td>{{$value.state}}</td>
      <td class="text-center">
        {{if $value.state=='已通过'}}
        <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})" >拒绝</a>
        {{else if $value.state=='已拒绝'}}
        <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
        {{else}}
        <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
        <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
        {{/if}}
        <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="del({{$value.id}})">删除</a>
      </td>
    </tr>
    {{/each}}
  </script>
  <script>
    $(function () {
      // 1.获取评论接口数据
      get_list();
    })
    // 1.显示评论列表
    function get_list(curr, limit) {
      let pagr_params = {
        page: curr,
        perpage: limit
      }
      myAjax('get', '/admin/comment/search', pagr_params, function (resData) {
        // console.log(resData);
        let resHtml = template('art_list', resData.data);
        $('#tbody').html(resHtml);
        // 此处resData中有一个totalCount的属性，代表服务器的评论数据总条数
        page_render(resData.data.totalCount, curr)
      })

    }
    // 2.渲染分页控件
    function page_render(totalCount, curr) {
      layui.use('laypage', function () {
        // 
        var laypage = layui.laypage;
        laypage.render({
          elem: 'pager',
          count: totalCount,
          curr: curr,
          limit: 6,
          jump: function (obj, first) {
            // obj.curr表示用户点击的当前页码
            // obj.limit表示此分页控件每页显示的条数
            // first 第一次将来是true，用户点击页码触发的时候 undefined
            if (!first) {
              get_list(obj.curr, obj.limit)
            }
          }
        })
      })
    }

    // 拒绝按钮的操作
    function reject(c_id) {
      layer.confirm('您确定拒绝该条数据吗？', { icon: 3, title: '提示' }, function (index) {
        myAjax('post', '/admin/comment/reject', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg);
            return;
          }
          get_list();
        })

        layer.close(index);
      });
    }
    // 审核通过按钮
    function pass(c_id) {
      layer.confirm('您确定通过该条数据吗？', { icon: 3, title: '提示' }, function (index) {
        myAjax('post', '/admin/comment/pass', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg);
            return;
          }
          get_list();
        })

        layer.close(index);
      });
    }
    // 删除按钮
    function del(c_id) {
      layer.confirm('您确定删除该条数据吗？', { icon: 3, title: '提示' }, function (index) {
        myAjax('post', '/admin/comment/delete', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg);
            return;
          }
          get_list();
        })

        layer.close(index);
      });
    }
  </script>
</body>

</html>